﻿Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath('Ext.ux', '../../../Extjs/ux/');
Ext.require([ 
        'Ext.selection.CellModel' ,   
	    'Ext.grid.*', 
	    'Ext.toolbar.Paging', 
	    'Ext.util.*', 
        'Ext.data.*',
	    'Ext.state.*'
       ,'Ext.form.*'
	       
       ,'Ext.selection.CheckboxModel'
       //,'Ext.ux.form.SearchField'
      // , 'Ext.ux.CheckColumn'
]); 


Ext.onReady(function () {
   // alert(1);
    Ext.QuickTips.init();
    function change(val) {
        if (val > 0) {
            return '<span style="color:green;">' + val + '</span>';
        } else if (val < 0) {
            return '<span style="color:red;">' + val + '</span>';
        }
        return val;
    }
    /**
    * Custom function used for column renderer
    * @param {Object} val
    */
    function pctChange(val) {
        if (val > 0) {
            return '<span style="color:green;">' + val + '%</span>';
        } else if (val < 0) {
            return '<span style="color:red;">' + val + '%</span>';
        }
        return val;
    }
    function afterEdit(e) {
        alert('edit');
    }
    Ext.Ajax.request({
        url: "RunAccoData.ashx",
        params: { prjtype: "PAW" },
        success: function (response, config) {
           
            //var json=Ext.util.JSON.decode(response.responseText);
            var json = eval("(" + response.responseText + ")");
            var cm = json.columnModel;
            var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
                clicksToEdit: 2,
                listeners: {
                    edit:{element:'el',
                         fn:function(editor,e){
                             var key =e.record.data["ID"];

                         }
                      }
                    }
            });
            //存储数据的单位
            var store = new Ext.data.JsonStore({
                data:json.data            //json格式的数据
                ,fields: json.fieldsNames   //字段列表
                ,idProperty:"ID"
            });
             
            //var SltModel = Ext.grid.CheckboxSelectionModel({ checkOnly: true });
            var grid = Ext.create('Ext.grid.Panel', {                
                store: store,
                //columns: [SltModel].contact(cm),                
                //columns: [Ext.create('Ext.grid.RowNumberer')].concat(cm),
                columns: cm,
                columnLines: true,                
                height: 300,
                width: 1100,
                title: '台账'
               , renderTo: "divRunAccContain"
               , viewConfig: {
                    stripeRows: true
//                    ,columnsText: '显示列'
//                    ,sortAscText: '升序' 
//                    ,sortDescText: '降序'
                }                
                ,frame: true,
                selType: 'rowmodel'

                //附加的插件
               , plugins: [cellEditing]
               ,

                //selModel: Ext.create('Ext.selection.CheckboxModel'),
               /*  listeners: {  
                        "edit": {  
                            fn: afterEdit,  
                            scope: this  }
                    },  
                    */
               
                /* , tbar: [{ text: "保存",
                    handler: function () {
                        var modified = store.modified;
                        //updateData(modified);
                            }
                        },
                      '-',
                      { text: "增加",
                          handler: function () {
                              var Plant = store.recordType;
                              var p = new Plant();
                          }
                      },
                      ' ',
                      { text: "删除",
                          handler: function () {
                              var selModel = grid.getSelectionModel();
                              if (selModel.hasSelection()) {
                                  Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
                                      if (button == "yes") {
                                          var selections = selModel.getSelections();
                                          Ext.each(selections, function (item) {
                                              store.remove(item);
                                              store.removed.push(item);
                                          })
                                      }
                                  })
                              }
                          }
                      }
                    ]*/
                    /*
                dockedItems: [{ 
	                    dock: 'top', 
	                    xtype: 'toolbar', 
	                    items: [{ 
	                        itemId: 'Button', 
	                        text:'显示所选', 
	                        tooltip:'Add a new row', 
	                        iconCls:'add', 
	                        handler:function(){ 
	                            var record = grid.getSelectionModel().getSelection(); 
	                            if(record.length == 0){ 
	                                Ext.MessageBox.show({ 
	                                    title:"提示", 
	                                    msg:"请先选择您要操作的行!", 
	                                    icon: Ext.MessageBox.INFO, 
	                                    buttons: Ext.Msg.OK 
	                                }) 
	                                return; 
	                            }else{ 
	                                var ids = ""; 
	                                for(var i = 0; i < record.length; i++){ 
	                                    ids += record[i].get("id") 
	                                    if(i<record.length-1){ 
	                                        ids = ids + ","; 
	                                    } 
	                                } 
	                                Ext.MessageBox.show({ 
	                                    title:"所选ID列表", 
	                                    msg:ids 
	                                    //icon: Ext.MessageBox.INFO 
	                                }) 
	                            } 
	                        } 
	                    },'-',{ 
	                        width: 300, 
	                        fieldLabel: '搜索', 
	                        labelWidth: 50, 
	                        xtype: 'searchfield', 
	                        store: store 
	                    }] 
	                }, { 
	                    dock: 'bottom', 
	                    xtype: 'pagingtoolbar', 
	                    store: store, 
	                    pageSize: 25, 
	                    displayInfo: true, 
	                    displayMsg: '显示 {0} - {1} 条，共计 {2} 条', 
	                    emptyMsg: '没有数据'
	                }]//dockedItems
                    */
            });//(grid)end            
            //store.load();
            grid.on('edit',function(editor, e){alert("edit");});
            grid.on('blur',function(editor, e){alert("blur");});
        },
        failure: function () {
            alert("有错误发生，请检查！");

        }
    })
})